package variabilitysmelldetection.plugin.popup.actions;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import variabilitysmelldetection.analyses.GodMethod;
import variabilitysmelldetection.measurement.Measurement;
import variabilitysmelldetection.measurement.models.ComponentItem;
import variabilitysmelldetection.measurement.models.MethodItem;
import variabilitysmelldetection.measurement.models.Project;
import variabilitysmelldetection.plugin.views.GodMethodView;
import de.ovgu.featureide.ahead.AheadComposer;
import de.ovgu.featureide.core.CorePlugin;
import de.ovgu.featureide.core.IFeatureProject;

public class DetectGodMethodAction implements IObjectActionDelegate {

	private Shell shell;

	/**
	 * Constructor for Action1.
	 */
	public DetectGodMethodAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();

		try {
			GodMethodView view = (GodMethodView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(GodMethodView.ID);

			if ((!selection.isEmpty()) && (selection instanceof IStructuredSelection)) {
				Object obj = ((IStructuredSelection) selection).getFirstElement();
				IResource res = null;

				if (obj instanceof IResource) {
					res = (IResource)obj;
				}else if (obj instanceof IJavaProject) {
					IJavaProject javaProject = (IJavaProject) obj;
					res = javaProject.getResource();				
				}

				IFeatureProject project = CorePlugin.getFeatureProject(res);				
				if (project != null && project.getComposerID().equals(AheadComposer.COMPOSER_ID)) {
					exec(project, view);
				}
			}
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	private void exec(IFeatureProject featureProject, GodMethodView view){
		Project project = new Project(featureProject);

		Measurement measurement = new Measurement(project);
		measurement.execute();

		GodMethod godMethod = new GodMethod();
		List<MethodItem> godMethods = new ArrayList<MethodItem>();
		
		List<ComponentItem> componentItems = project.getClassItems();
		for (ComponentItem c : componentItems) {
			List<MethodItem> methodItems = c.getMethods();

			for (MethodItem m : methodItems) {
				if (godMethod.hasGodMethod(m)) {
					godMethods.add(m);
				}
			}
		}
		
		if (!godMethods.isEmpty()) {
			view.show(featureProject, project, godMethods);

		} else {
			MessageDialog.openInformation(shell, 
					"VSD God Method", 
					"The SPL '"+project.getName()+"' does not have methods with God Method.");
			view.show(featureProject, project, new ArrayList<MethodItem>());
		}

	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

}
